/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaFormItem } from "@mineadmin/form";
//@ts-ignore
import MaUploadImage from "@/components/ma-upload-image/index.vue";
//@ts-ignore
import RemoteSelect from "@/components/ma-remote-select/index.vue";
import { page as categoryPage, CourseCategoryVo } from "@/modules/course/api/category";
import { CourseTaglVo, page as tagPage } from "@/modules/course/api/tag";
import { CourseVo } from "@/modules/course/api/manage";

export default function getFormItems(
  formType: "add" | "edit" = "add",
  t: any,
  model: CourseVo
): MaFormItem[] {
  if (formType === "add") {
    // model.password = "123456";
  }
  //@ts-ignore
  model.category_ids = model.categorys?.map(c=>c.id) || [];
  //@ts-ignore
  model.tag_ids = model.tags?.map(c=>c.id) || [];
  console.log(model)
  return [
    {
      label: () => t("course-manage.thumb"),
      prop: "thumb",
      render: () => MaUploadImage,
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: t("course-manage.thumb") }),
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: t("form.requiredInput", { msg: t("course-manage.thumb") }),
          },
        ],
      },
    },
    {
      label: () => t("course-manage.title"),
      prop: "title",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", { msg: t("course-manage.title") }),
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: t("form.requiredInput", { msg: t("course-manage.title") }),
          },
        ],
      },
    },
    {
      label: () => t("course-category.title"),
      prop: "category_ids",
      render: () => RemoteSelect,
      cols: { md: 12 },

      renderProps: {
        class: "w-full",
        placeholder: t("form.pleaseSelect", {
          msg: t("course-category.title"),
        }),
        multiple: true,
        api: () => new Promise((resolve) => resolve(categoryPage({ page_size: 999 }))),
        dataHandle: (response: any) => {
          return response.data.list?.map((item: CourseCategoryVo) => {
            return { label: `${item.name}`, value: item.id };
          });
        },
      },
    },
    {
      label: () => t("course-tag.title"),
      prop: "tag_ids",
      cols: { md: 12 },
      render: () => RemoteSelect,
      renderProps: {
        multiple:true,
        class: "w-full",
        placeholder: t("form.pleaseSelect", {
          msg: t("course-tag.title"),
        }),
        api: () => new Promise((resolve) => resolve(tagPage({ page_size: 999 }))),
        dataHandle: (response: any) => {
          return response.data.list?.map((item: CourseTaglVo) => {
            return { label: `${item.name}`, value: item.id };
          });
        },
      },
    },
    {
      label: () => t("course-manage.introduce"),
      prop: "introduce",
      render: "input",
      renderProps: {
        placeholder: t("form.pleaseInput", {
          msg: t("course-manage.introduce"),
        }),
        type: "textarea",
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: t("form.requiredInput", {
              msg: t("course-manage.introduce"),
            }),
          },
        ],
      },
    },

    {
      label: () => t("course-manage.score_option.micro"),
      prop: "score_option.micro",
      render: "input",
      cols: { md: 8 },
      renderProps: {
        placeholder: t("form.pleaseInput", {
          msg: t("course-manage.score_option.micro"),
        }),
        type: "number",
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: t("form.requiredInput", {
              msg: t("course-manage.score_option.micro"),
            }),
          },
        ],
      },
    },
    {
      label: () => t("course-manage.score_option.test"),
      prop: "score_option.test",
      render: "input",
      cols: { md: 8 },
      renderProps: {
        placeholder: t("form.pleaseInput", {
          msg: t("course-manage.score_option.test"),
        }),
        type: "number",
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: t("form.requiredInput", {
              msg: t("course-manage.score_option.test"),
            }),
          },
        ],
      },
    },
    {
      label: () => t("course-manage.score_option.live"),
      prop: "score_option.live",
      render: "input",
      cols: { md: 8 },
      renderProps: {
        placeholder: t("form.pleaseInput", {
          msg: t("course-manage.score_option.live"),
        }),
        type: "number",
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: t("form.requiredInput", {
              msg: t("course-manage.score_option.live"),
            }),
          },
        ],
      },
    },
    {
      label: () => t("course-manage.is_publish"),
      prop: "is_publish",
      render: "switch",
      renderProps: {
        placeholder: t("form.pleaseInput", {
          msg: t("course-manage.is_publish"),
        }),
        activeValue: 1,
        inactiveValue: 0,
      },
      itemProps: {
        rules: [
          {
            required: true,
            message: t("form.requiredInput", {
              msg: t("course-manage.is_publish"),
            }),
          },
        ],
      },
    },
  ];
}
